package com.apps.saspect.impl;


import com.apps.saspect.SuportLockModel;
import com.apps.sconfig.SDevFastConfig;
import com.apps.sexctption.ConfigException;
import com.apps.sexctption.NullException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
@Configuration
public class SuportLockModelAspect {

    @Autowired
    private SDevFastConfig sDevFastConfig;

    @Pointcut("@annotation(com.apps.saspect.SuportLockModel)")
    public void pointcut() {

    }

    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint)
    {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SuportLockModel lockModel=method.getDeclaredAnnotation(SuportLockModel.class);
        if(null == lockModel)
            throw new NullException("没有找到@SuportLockModel注解");
        if(sDevFastConfig.getLockType() != lockModel.type())
            throw new ConfigException("该方法不支持在"+ sDevFastConfig.getLockType().toString()+"模式下运行");

    }


}
